package com.yunzai.generic.domain;

import com.alibaba.fastjson2.JSONObject;

public class JSONResult extends JSONObject {

    public static JSONResult success() {
        return success(200, "操作成功！", null);
    }

    public static JSONResult success(String msg) {
        return success(200, msg, null);
    }

    public static JSONResult success(int code, String msg) {
        return success(code, msg, null);
    }

    public static JSONResult success(int code, String msg, Object data) {
        return getJSONResult(code, msg, data);
    }

    public static JSONResult success(String msg, Object data) {
        return success(200, msg, data);
    }

    public static JSONResult success(Object data) {
        return success(200, "操作成功！",data);
    }

    public static JSONResult error() {
        return error(500, "操作失败！", null);
    }

    public static JSONResult error(String msg) {
        return error(500, msg, null);
    }

    public static JSONResult error(int code, String msg) {
        return error(code, msg, null);
    }

    public static JSONResult error(int code, String msg, Object data) {
        return getJSONResult(code, msg, data);
    }

    public static JSONResult error(String msg, Object data) {
        return error(500, msg, data);
    }

    public static JSONResult error(Object data) {
        return error(500, "操作失败！", data);
    }

    public static JSONResult toJSONResult(int number) {
        return number > 0 ? success() : error();
    }

    private static JSONResult getJSONResult(int code, String msg, Object data) {
        JSONResult jsonResult = new JSONResult();
        jsonResult.put("code", code);
        jsonResult.put("msg", msg);
        jsonResult.put("data", data);
        return jsonResult;
    }
}
